include('shared.lua')

ENT.RenderGroup = RENDERGROUP_OPAQUE

function ENT:Draw()
	self.Entity:DrawModel()
end 

-- dist = 93.9
-- height = 46.45
-- scale = range / (numents * propsize)
-- size of propane canister muzle = 7.717
-- if DPS of propane canister is = 10
-- how can we turn 46.45 into 7.717 if dps is 10
-- self.DPS / 10 * 7.717 / 46.45 or
-- self.DPS * 0.0166
-- pos + (2i-1) * dist * scale.z * Entity:GetUp()

-- Material("sprites/light_glow02")

function ENT:GenerateCSEffect()
	--local angles = self:GetAngles() + Angle(90, 0, 0)
	local angles = self:GetAngles()
	
	self.numents = 6
	self.range = self:GetNetworkedInt("range")
	self.muzzle = self:GetNetworkedVector("muzzle")
	self.DPS = self:GetNetworkedInt("dps")
	
	local scale = Vector(1, 1, 0) * self.DPS * 0.0166 / 4 + Vector(0, 0, 1) * self.range / (self.numents * 93.9)
	local pos = self.Entity:GetPos() + self.muzzle.x * self.Entity:GetForward() + self.muzzle.y * self.Entity:GetRight() + self.muzzle.z * self.Entity:GetUp()
	self.extscale = scale
	
	self.sitp_csents = {}
	self.sitp_csentsint = {}
	
	-- outer shell
	for i = 1, self.numents do
		local beament = ents.Create( "prop_physics" )
		local offset = pos + (2 * i - 2) * (93.9) * scale.z * self.Entity:GetUp() / 2
		beament:SetModel("models/hunter/tubes/tube1x1x2.mdl")
		beament:SetMaterial("models/alyx/emptool_glow")
		beament:SetAngles(angles)
		beament:SetModelScale(scale)
		beament:SetColor(100, 155, 155, 0)
		beament:SetPos(offset)
		beament:Spawn()
		
		beament:SetParent(self)
		
		self.sitp_csents[i] = beament
	end
	
	scale = Vector(1, 1, 0) * self.DPS * 0.0166 / 8 + Vector(0, 0, 1) * self.range / (self.numents * 93.9)
	self.intscale = scale
	
	for i = 1, self.numents do
		local beament = ents.Create( "prop_physics" )
		local offset = pos + (2 * i - 2) * (93.9) * scale.z * self.Entity:GetUp() / 2
		beament:SetModel("models/hunter/tubes/tube1x1x2.mdl")
		beament:SetMaterial("models/debug/debugwhite")
		beament:SetAngles(angles)
		beament:SetModelScale(scale)
		beament:SetColor(255, 255, 255, 0)
		beament:SetPos(offset)
		beament:Spawn()
		
		beament:SetParent(self)
		
		self.sitp_csentsint[i] = beament
	end
	
	self.DPSPerc = 0
	self.cseffectgen = true
end

function ENT:UpdateCSEffect()
	self.isfiring = 1 - math.floor(math.Clamp(self:GetNetworkedInt("sizeof"), 0, 1))
	
	self.DPSPerc = math.Clamp(self.DPSPerc + (-1)^(self.isfiring) * 0.2, 0, 1)
	
	if(self.DPSPerc > 0) then
		local flicker = math.random() * 0.1 + 0.9
		local muz = self.muzzle
		local pos = self.Entity:GetPos() + muz.x * self.Entity:GetForward() + muz.y * self.Entity:GetRight() + muz.z * self.Entity:GetUp()
	
		local trace = {}
			trace.start = pos + self.Entity:GetUp() * 5
			trace.endpos = pos + self.Entity:GetUp() * self.range
			trace.filter = self.Entity 
		local tr = util.TraceLine( trace )
		
		local dist = 5 + tr.Fraction * (self.range - 5)
		local scale = Vector(1, 1, 0) * self.DPS * self.DPSPerc * 0.0166 / 4 * flicker + Vector(0, 0, 1) * dist / (self.numents * 93.9)
		local scalet = Vector(1, 1, 0) * self.DPS * self.DPSPerc * 0.0166 / 8 + Vector(0, 0, 1) * dist / (self.numents * 93.9)
		
		for i = 1, self.numents do
			local offset = pos + (2 * i - 2) * (93.9) * scale.z * self.Entity:GetUp() / 2
			self.sitp_csents[i]:SetPos(offset)
			self.sitp_csents[i]:SetModelScale(scale)
			self.sitp_csents[i]:SetColor(100, 155, 255, 180 * self.DPSPerc * flicker)
			
			self.sitp_csentsint[i]:SetPos(offset)
			self.sitp_csentsint[i]:SetModelScale(scalet)
			self.sitp_csentsint[i]:SetColor(255, 255, 255, 185 * self.DPSPerc)
		end
	else
		for i = 1, self.numents do
			self.sitp_csents[i]:SetColor(100, 155, 255, 0)
			self.sitp_csentsint[i]:SetColor(255, 255, 255, 0)
		end
	end
end

function ENT:Think()
	if(!self.sitp_csrender) then
		self.sitp_csrender = self:GetNetworkedBool("canrender")
	else
		if(self.sitp_csrender == false) then
			self.sitp_csrender = self:GetNetworkedBool("canrender")
		else
			if(!self.cseffectgen) then
				self:GenerateCSEffect()
			end
		end
	end
	
	if(self.cseffectgen) then
		self:UpdateCSEffect()
	end
	
	if (CurTime() >= (self.NextRBUpdate or 0)) then
	    self.NextRBUpdate = CurTime() + 0.1
		Wire_UpdateRenderBounds(self.Entity)
	end
end

function ENT:OnRemove()
	for i = 1, self.numents do
		self.sitp_csents[i]:Remove()
		self.sitp_csentsint[i]:Remove()
	end
end